package com.ruoyi.quartz.task;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.Constant;
import com.ruoyi.framework.config.ThreadExecutorPoolConfig;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.V2.inter.mapper.ApiTaskPoolMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.V2.inter.mapper.JymtaskMapper;
import com.ruoyi.system.V2.inter.mapper.SalePartnerMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.outto.service.IOutApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

@Slf4j
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
//@EnableAsync
public class DelScheduleTask {


    @Resource
    private ApiTaskPoolMapper apiTaskPoolMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private IOutApi iOutApi;
    @Resource
    private AccountInfoMapper accountInfoMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ThreadExecutorPoolConfig threadExecutorPoolConfig;


    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    public void configureTasksDel() {

        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());

        List<Callable<Object>> tasks = new ArrayList<>();
        List<Map<String, Object>> accountInfoList = accountInfoMapper.buchang();
        for (Map map : accountInfoList) {
            tasks.add(() -> {
                Long id = MapUtils.getLong(map, "id");
                ArtRec artRec = new ArtRec();
                artRec.setAccountId(id);
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRec);
                for (ArtRec rec : artRecs) {
                    if(rec.getConfig().toString().contains("YSY")||rec.getConfig().toString().contains("THG")){
                        Thread.sleep(6000l);
                    }
                    String goods = "del"+rec.getAccountId().toString()+rec.getConfig().toString();
                    //查询缓存是否已删除
                    if(Objects.isNull(redisCache.getCacheObject(goods))) {
                        //添加缓存
                        redisCache.setCacheObject(goods, "fangchongfu", 1, TimeUnit.MINUTES);
                        iOutApi.del(id, rec.getConfig(), 1, null);
                    }
                    apiTaskPoolMapper.deleteApiTaskPoolByConfigAndId(id,rec.getConfig(),"del");
                }
                return null;
            });
        }
        try {
            threadExecutorPoolConfig.threadPoolTaskExecutor().invokeAll(tasks);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //交易猫任务
//    @Scheduled(cron = "0/20 * * * * ?")
    public void configureTasksJYM() throws InterruptedException {

        System.err.println("交易猫: " + LocalDateTime.now());

        /**
         * 1、循环任务池信息
         * 2、调用接口任务，成功的，修改状态
         */
        ApiTaskPool apiTaskPool = new ApiTaskPool();
        apiTaskPool.setStatus(3l);
        apiTaskPool.setCountt(1);
        apiTaskPool.setSize(10l);
        apiTaskPool.setPartCode("JYM");

        List<ApiTaskPool> apiTaskPoolList = apiTaskPoolMapper.selectApiTaskPoolList(apiTaskPool);

        for (ApiTaskPool taskPool : apiTaskPoolList) {


            GoodsInfo goodsInfo = goodsInfoMapper.selectGoodsInfoById(taskPool.getAccountId());
            if (Objects.nonNull(goodsInfo)) {
                iOutApi.del(goodsInfo.getId(), Long.parseLong(taskPool.getConfig()), 1, taskPool.getId());
            } else {
                taskPool.setStatus(0l);
                apiTaskPoolMapper.updateApiTaskPool(taskPool);
            }
        }
    }

}
